package logic

import (
	"context"
	"order-srv/model/mysql"

	"order-srv/internal/svc"
	"order-srv/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type GenerateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGenerateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GenerateOrderLogic {
	return &GenerateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GenerateOrderLogic) GenerateOrder(in *order.GenerateOrderRequest) (*order.GenerateOrderResponse, error) {
	// todo: add your logic here and delete this line
	o := mysql.Order{
		UserId:  int32(in.UserId),
		OrderSn: in.OrderSn,
		Status:  1,
		PayType: 1,
		PayTime: 0,
		Total:   in.Total,
	}
	err := o.CreateOrder()
	if err != nil {
		logx.Error("生成订单失败", err)
		return nil, err
	}
	return &order.GenerateOrderResponse{OrderId: int64(o.ID)}, nil
}
